package com.example.controller.modules;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfObject;
import com.itextpdf.text.pdf.PdfOutline;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
//import org.apache.poi.xwpf.converter.pdf.PdfOptions;
//import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;


//import com.aspose.words.Document;
//import com.aspose.words.SaveFormat;



/**
 * @Description:
 * @Date：Create in 2024-11-26 09:48
 * @Author: 刘雨
 */
@Controller
@RequestMapping("convertPdfAndWord")
public class PdfAndWordController {

    private final static Logger log = LoggerFactory.getLogger(PdfAndWordController.class);


    @GetMapping("/pdfToWord.do")
    public void getPdfToWord(MultipartFile pdfFile){
        log.info("PDF开始转换WORD:/pdfToWord.do");
        log.info("文件名称："+ pdfFile.getName());
        log.info("文件名称："+ pdfFile.getOriginalFilename());
        PDDocument file_pdf = new PDDocument();


//        //去掉转成PDF后的文件上方有Aspose的对应的版权信息，需要使用授权下方的license。
//        try {
//            // 设置word license
//            ClassPathResource classPathResource = new ClassPathResource("license/license-word.xml");
//            InputStream is = classPathResource.getInputStream();
//            License aposeLic = new License();
//            aposeLic.setLicense(is);
//
//            // 设置pdf license
//            ClassPathResource classPathResource2 = new ClassPathResource("license/license-pdf.xml");
//            InputStream licensePdf = classPathResource2.getInputStream();
//            com.aspose.pdf.License aposeLicPdf = new com.aspose.pdf.License();
//            aposeLicPdf.setLicense(licensePdf);
//            is.close();
//            licensePdf.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    @GetMapping("/wordToPdf.do")
    public void getWordToPdf(MultipartFile wordFile){
        log.info("WORD开始转换PDF:/wordToPdf.do");

        log.info("文件底地址："+ wordFile.getName());
        log.info("文件底地址："+ wordFile.getOriginalFilename());
        log.info("文件底地址："+ PdfAndWordController.class.getResource("/").toString().replace("file:/", ""));

        File file = new File(wordFile.getOriginalFilename());
        try {
            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(wordFile.getBytes());

            int lastIndexOf = wordFile.getOriginalFilename().lastIndexOf('.');
            String suffixTitle = wordFile.getOriginalFilename().substring(0, lastIndexOf);
            String outFileName = suffixTitle + ".pdf";
            // PDF 输出流:
            FileOutputStream fileOutStream = new FileOutputStream(outFileName);



            // WORD 输入流对象：
            XWPFDocument wordDocument = new XWPFDocument(new FileInputStream(file));
            file.delete();
            PDDocument pdfDocument = new PDDocument();

            // 遍历Word文档中的段落并添加到PDF

            for (XWPFParagraph paragraph : wordDocument.getParagraphs()) {
                PDPage page = new PDPage(PDRectangle.A4);
                pdfDocument.addPage(page);
                PDPageContentStream contentStream = new PDPageContentStream(pdfDocument, page);
                contentStream.beginText();
                contentStream.setFont(PDType1Font.HELVETICA, 12);
                contentStream.moveTextPositionByAmount(20, 700);
                contentStream.drawString(paragraph.getText());
                contentStream.endText();
                contentStream.close();
            }

            // 关闭内容流，并保存文档
            pdfDocument.save(fileOutStream);
            pdfDocument.close();
            fileOutStream.close();
            wordDocument.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        // 创建一个PDF文档：
        PDDocument pdf = new PDDocument();




    }

    @GetMapping("/wordToPdf.two")
    public void getWordToPdf_2(MultipartFile wordFile){

        Document document = new Document();

        File file = new File(wordFile.getOriginalFilename());
        try {
            FileOutputStream outputStream = new FileOutputStream(file);
            outputStream.write(wordFile.getBytes());

            int lastIndexOf = wordFile.getOriginalFilename().lastIndexOf('.');
            String suffixTitle = wordFile.getOriginalFilename().substring(0, lastIndexOf);
            String outFileName = suffixTitle + ".pdf";
            // PDF 输出流:
            FileOutputStream fileOutStream = new FileOutputStream(outFileName);

            PdfWriter.getInstance(document, fileOutStream);
            document.open();
            document.add(new Paragraph("Hello, iText!"));
            document.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
